<?php
/**
 * @ClassName: WechatLogic
 * @Description: 微信逻辑
 * @author: wyaohu
 * @Date: 2024-09-14 11:11
 * @Version: V1.0.0
 */

namespace app\common\logic;

use GuzzleHttp\Client;

class WechatLogic
{
    /**
     * 微信公众号配置
     * @var string[]
     */
    protected $mpConfig = [
        'app_id' => 'wx2f04d6a3159eb1fe',
        'secret' => 'af4bdbe9475e2a793d604b8c70eb8a02',
        'response_type' => 'array',
    ];

    /**
     * 微信小程序配置
     * @var string[]
     */
    protected $miniConfig = [
        'app_id' => 'wx81cdfa13e414c022',
        'secret' => '8ce46a802107a7fe965ac0cf0807cf91',
        'response_type' => 'array',
    ];

    /**
     * 微信APP配置
     * @var string[]
     */
    protected $appConfig = [
        'app_id' => 'wx0f02ec34dc1b3119',
        'secret' => 'b112f5a7a7f384de2395bbf347b15118',
        'response_type' => 'array',
    ];

    /**
     * 解析微信小程序 wx.login 登录获得的code
     * @param $code
     * @return mixed|void
     * @throws \GuzzleHttp\Exception\GuzzleException
     */
    function getMiniSessionCode($code = '')
    {
        try {
            $uri = 'https://api.weixin.qq.com/sns/jscode2session?appid=' . $this->miniConfig['app_id'] .
                '&secret=' . $this->miniConfig['secret'] . '&js_code=' . $code . '&grant_type=authorization_code';
            $client = new Client();
            $response = $client->get($uri);
            $data = json_decode($response->getBody()->getContents(), true);

            trace($data);

            if (isset($data['unionid'])) {
                $data['code'] = 1;
            }
            return $data;
        } catch (\Exception $e) {
            return ['code' => 0, 'msg' => '登录失败'];
        }
    }

    /**
     * 使用授权码获取access_token和openid
     * @param $code
     * @return mixed|void
     * @throws \GuzzleHttp\Exception\GuzzleException
     */
    function getMpSessionCode($code = '')
    {
        try {
            $uri = 'https://api.weixin.qq.com/sns/oauth2/access_token?appid=' . $this->mpConfig['app_id'] .
                '&secret=' . $this->mpConfig['secret'] . '&code=' . $code . '&grant_type=authorization_code';
            $client = new Client();
            $response = $client->get($uri);
            $data = json_decode($response->getBody()->getContents(), true);
            trace($data);
            if (isset($data['openid'])) {
                $data['code'] = 1;
            }
            return $data;
        } catch (\Exception $e) {
            return ['code' => 0, 'msg' => '登录失败'];
        }
    }

    /**
     * 获取手机号
     * @param $iv
     * @param $encryptedData
     * @param $session_key
     * @return mixed|string
     */
    function getPhone($iv, $encryptedData, $session_key)
    {
        $aesKey = base64_decode($session_key);
        $aesIV = base64_decode($iv);
        $aesCipher = base64_decode($encryptedData);
        $result = openssl_decrypt($aesCipher, "AES-128-CBC", $aesKey, 1, $aesIV);
        $phoneNumber = json_decode($result, true);
        if ($phoneNumber) {
            return $phoneNumber['purePhoneNumber'];
        } else {
            return "";
        }
    }

}